﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using Distribution.General;
using Entities;

namespace DistributionApplication.Controls
{
    public partial class MultipleCameraControl : UserControl
    {
        private Camera m_Camera;
        Int32 m_ImageCounter;
        List<EImage> m_TakedImages;
        #region - Properties -

        public string TempPhotoFileName { get; set; }

        #endregion

        public MultipleCameraControl()
        {
            InitializeComponent();
            m_Camera = Camera.GetInstance();
            m_TakedImages = new List<EImage>();
        }

        private void EnableCamera()
        {
            Cursor.Current = Cursors.WaitCursor;
            pictureBoxPreviewPhoto.Image = null;
            if (m_Camera.PowerState == PowerStates.Off)
                m_Camera.On(35, 70);
            else
                m_Camera.SetCameraFinder((int)PowerStates.On, 35, 70);
            Cursor.Current = Cursors.Default;

        }

        public void DisableCamera()
        {
            m_Camera.Off(0, 0);
        }

        public bool IsPowerOn()
        {
            return (m_Camera.PowerState == PowerStates.On);
        }

        private void DisableCameraFinder()
        {
            m_Camera.SetCameraFinder((int)PowerStates.Off, 0, 0);
        }

        public void ActiveCamera()
        {
            EnableCamera();

        }

        private bool TakePhoto(string FileName)
        {
            lblTakingPhoto.Visible = true;
            Application.DoEvents();
            int i = 0;
            bool _Taken = false;
            while (i < 3 && !_Taken)
            {
                DisableCameraFinder();
                m_Camera.TakePhoto(FileName, 1);

                if (File.Exists(FileName))
                    _Taken = true;
                else
                {
                    GC.Collect();
                    if (i == 2)
                    {
                        DisableCamera();
                        MessageBox.Show(Constants.K_AlertImposibleTakePhoto, PPIConstants.K_AppName);
                        EnableCamera();
                        return false;
                    }
                }
                i++;
            }
            lblTakingPhoto.Visible = false;
            Application.DoEvents();
            return true;
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (m_Camera.PowerState == PowerStates.Off)
                return;

            string fileName = TempPhotoFileName + m_ImageCounter + ".jpg";
            string pathfileName = PPIConstants.K_ImagesFolder + fileName;
            if (TakePhoto(pathfileName))
            {
                m_ImageCounter++;
                DisableCamera();
                EnableCamera();
                m_TakedImages.Add(new EImage(fileName));
                lstImages.Items.Add(fileName);
            }
        }

        private void lstImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstImages.SelectedIndex > -1)
            {
                EImage _tempPhoto = m_TakedImages[lstImages.SelectedIndex];
                DisableCamera();
                pictureBoxPreviewPhoto.Image = String.IsNullOrEmpty(_tempPhoto.FileName) || !File.Exists(PPIConstants.K_ImagesFolder + _tempPhoto.FileName) ? null : ImageHelper.GetImage(PPIConstants.K_ImagesFolder + _tempPhoto.FileName);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            mitDelete_Click(sender, e);
            EnableCamera();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            pictureBoxPreviewPhoto.Image = null;
            DisableCamera();
            EnableCamera();
        }

        public List<EImage> GetTakenImages()
        {
            return m_TakedImages;
        }

        public void SetTakenImages(List<EImage> images)
        {
            m_TakedImages = images;
            lstImages.Items.Clear();
            foreach (EImage i in images)
            {
                lstImages.Items.Add(i.FileName);
            }
            m_ImageCounter = images.Count;

        }

        public void SelectPicture(int index)
        {
            if (lstImages.Items.Count > 0)
                lstImages.SelectedIndex = index;
        }

        public void Reset()
        {
            m_ImageCounter = 0;
            lstImages.Items.Clear();
            m_TakedImages = new List<EImage>();

            //ActiveCamera();
        }

        public void Clean()
        {
            m_ImageCounter = 0;
            lstImages.Items.Clear();
            m_TakedImages = new List<EImage>();
        }

        private void mitDelete_Click(object sender, EventArgs e)
        {
            if (lstImages.SelectedIndex > -1)
            {
                EImage _tempPhoto = m_TakedImages[lstImages.SelectedIndex];
                _tempPhoto.IsDeleted = true;
                DisableCamera();
                pictureBoxPreviewPhoto.Image = String.IsNullOrEmpty(_tempPhoto.FileName) || !File.Exists(PPIConstants.K_ImagesFolder + _tempPhoto.FileName) ? null : ImageHelper.GetImage(PPIConstants.K_ImagesFolder + _tempPhoto.FileName);
                m_TakedImages.RemoveAt(lstImages.SelectedIndex);
                lstImages.Items.RemoveAt(lstImages.SelectedIndex);
            }
        }

        private void pictureBoxPreviewPhoto_Click(object sender, EventArgs e)
        {
            if (pictureBoxPreviewPhoto.Image != null)
            {
                contextMenuImage.Show(pictureBoxPreviewPhoto, pictureBoxPreviewPhoto.Location);
            }
        }
        private void mit320x240_Click(object sender, EventArgs e)
        {
            SetZoom(320, 240);
        }
        private void SetZoom(int width, int height)
        {
            PictureBox _PbxImage = new PictureBox();
            _PbxImage.Location = new Point(0, 0);
            _PbxImage.Size = new Size(width, height);
            _PbxImage.SizeMode = PictureBoxSizeMode.StretchImage;
            _PbxImage.Image = pictureBoxPreviewPhoto.Image;
            Form _FormZoomImage = new Form();
            #region Configure form
            _FormZoomImage.SuspendLayout();
            _FormZoomImage.AutoScaleDimensions = new SizeF(96F, 96F);
            _FormZoomImage.AutoScaleMode = AutoScaleMode.Dpi;
            _FormZoomImage.AutoScroll = true;
            _FormZoomImage.ClientSize = new Size(238, 297);
            _FormZoomImage.Controls.Add(_PbxImage);
            _FormZoomImage.MaximizeBox = false;
            _FormZoomImage.MinimizeBox = false;
            _FormZoomImage.Text = "PPI | Zoom Image";
            _FormZoomImage.TopMost = true;
            _FormZoomImage.ResumeLayout(false);
            #endregion Configure form
            Application.DoEvents();
            _FormZoomImage.ShowDialog();
        }

        private void mit640X480_Click(object sender, EventArgs e)
        {
            SetZoom(640, 480);
        }




    }
}
